package cn.byapps.shiro.config;

import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {

	/**
	 * 1. 创建UserRealm
	 * 
	 * @return
	 */
	@Bean
	public AuthorizingRealm getRealm() {
		return new UserRealm();
	}

	/**
	 * 2. 创建 DefaultWebSecurityManager
	 * 
	 * @param realm
	 * @return
	 */
	@Bean
	public DefaultWebSecurityManager getDefaultWebSecurityManager(AuthorizingRealm realm) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		// 关联
		securityManager.setRealm(realm);
		return securityManager;
	}

	/**
	 * 3. 创建ShiroFilter工厂
	 * 
	 * @param securityManager
	 * @return
	 */
	public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		// 设置安全管理器（权限）
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		return shiroFilterFactoryBean;
	}
}
